<?php

/**
 * Blog module bootstrap
 *
 * Adapted from jasoneisen's ZFSecurity project
 * http://code.google.com/p/zfsecurity/
 *
 * I merely made the bootstrap and some minor adjustments here and there.
 * This whole blog thing was basically made by him.
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @see       http://code.google.com/p/zfsecurity/source/browse/trunk/demo/application/bootstrap.php
 * @version   $Id: Bootstrap.php 38 2009-02-18 17:12:39Z monzee $
 */
class Blog_Bootstrap extends Mz_Module_Bootstrap
{
    static protected $_config;

    static public function preRoute($module, $path)
    {
        self::$_config = new Zend_Config_Xml($path . '/config/config.xml');

        // Add the routes
        $frontController = Zend_Controller_Front::getInstance();
        
        $router = $frontController->getRouter();
        $router->addConfig(self::$_config->routes);
        
        $frontController->setRouter($router);
    }

    public function init()
    {
        // Get the config object
        $config = self::$_config;

        // Set up the database
        $profiler = new Doctrine_Connection_Profiler();
        Zend_Registry::set('Doctrine_Connection_Profiler', $profiler);

        // change this to use the dsn in the config
        $dsn = 'sqlite:///' . $this->pathTo('data/development.db');

        Doctrine_Manager::connection($dsn)
            ->setAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER, true)
            ->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true)
            ->setListener($profiler);

        $manager = Doctrine_Manager::getInstance();
        $manager->setAttribute('use_dql_callbacks', true);
        $manager->setAttribute('model_loading', 'conservative');

        // Set up the front controller
        $frontController = Zend_Controller_Front::getInstance();
        $frontController->registerPlugin(new App_Controller_Plugin_Rest());

        // Set up the layout
        if (null === Zend_Layout::getMvcInstance()) {
            Zend_Layout::startMvc();
        }

        $layout = Zend_Layout::getMvcInstance();
        $layout->setLayoutPath($this->pathTo('layouts'));
        $layout->getInflector()->setTarget(':script/layout.:suffix');
        $layout->setLayout('default');

        // Set up the view
        $view = $layout->getView();
        $view->addScriptPath($this->pathTo('layouts/default'))
             ->addHelperPath($this->pathTo('library/App/View/Helper'), 'App_View_Helper')
             ->addHelperPath($this->pathTo('views/helpers'), 'Blog')
             ->headTitle($config->layout->title)->setSeparator(' / ');
        $view->placeholder('branding')->set($config->layout->branding);

        Zend_Dojo::enableView($view);

        // add ./models to the include path. we wouldn't normally do this, instead
        // the models should be handled properly by the load() method. this is just for
        // convenience since doctrine wouldn't be able to autodiscover models if
        // our module models followed the ZF recommended naming scheme (i.e. 'Blog_User',
        // 'Blog_Post' and 'Blog_Comment'. CAVEAT: it might cause conflicts with
        // classes defined in the main models directory of the application.
        set_include_path($this->pathTo('models') . PATH_SEPARATOR . get_include_path());
    }

    /**
     * We have to override this because the Doctrine-generated base model classes
     * do not follow the PEAR naming convention.
     *
     * @param string $class Class name
     * @return bool
     */
    static public function load($class)
    {
        $found = parent::load($class);

        if ($found) {
            return true;
        }

        $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';

        if ('base' == strtolower(substr($class, 0, 4)) && '_' != substr($class, 4, 1)) {
            // doctrine generated class path
            $file = self::pathTo('models/generated/' . $file);
        }

        @include_once $file;
    }

}
